using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;

namespace Stopwatch
{
    class RunReport
    {

        public void SaveReport(DataSet dsRunData)
        {

            try
            {
                CheckReportFolder();

                string sFileName = DateTime.Now.ToString("mmddyyyyhhMMss.runrep");

                sFileName += ".xml";

                StringBuilder sbXMLFile = new StringBuilder(Common.PATH_REPORTS);
                
                sbXMLFile.Append(sFileName);

                dsRunData.WriteXml(sbXMLFile.ToString());

            }
            catch (Exception ex)
            {
                
                throw ex;
            }

        }


        public void GetSummaryDataFromXML(string XMLFile, ref string sAverage,
             ref string sFastest, ref string sSlowest, ref string sLapCount, ref string sRepNumber)
        {

            XmlReader xmlReader = null;

            try
            {

                xmlReader = XmlReader.Create(XMLFile);

                //Get fastest lap from XML
                if (xmlReader.ReadToFollowing("Fastest"))
                    sFastest = xmlReader.ReadString();

                //Get slowest lap from XML
                if (xmlReader.ReadToFollowing("Slowest"))
                    sSlowest = xmlReader.ReadString();

                //Get Average lap from XML
                if (xmlReader.ReadToFollowing("Average"))
                    sAverage = xmlReader.ReadString();

                //Get lap count from XML
                if (xmlReader.ReadToFollowing("LapCount"))
                    sLapCount = xmlReader.ReadString();

                //Get Report number from XML
                if (xmlReader.ReadToFollowing("ReportNumber"))
                    sRepNumber = xmlReader.ReadString();


            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                xmlReader.Close();
            }
        }

        public DataSet GetLapSummaryDetails(string XMLPath)
        {

            DataSet dsLapSummary = new DataSet("RunReport");

            try
            {
                dsLapSummary.ReadXml(XMLPath);
                return dsLapSummary;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dsLapSummary.Dispose();
            }
        
        }

        public FileInfo[] GetReportsFromFolder()
        {

            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(Common.PATH_REPORTS);

                if (!dirInfo.Exists)
                {
                    dirInfo.Create();
                }

                FileInfo[] fInfo = dirInfo.GetFiles("*.xml");

                return fInfo;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        
        }

        public int GetNextReportNumber()
        {
            XmlReader xmlReader = null;

            try
            {
                int iNextReportNumber = 0;

                DirectoryInfo dirInfo = new DirectoryInfo(Common.PATH_REPORTS);

                IComparer IDateComparer = new DateComparerDes();

                FileInfo[] fInfo = dirInfo.GetFiles("*.xml");

                if (fInfo.Length > 0)
                {

                    Array.Sort(fInfo, IDateComparer);

                    string sXMLFile = fInfo[0].FullName;

                    xmlReader = XmlReader.Create(sXMLFile);

                    if (xmlReader != null)
                    {
                        //Get Report number from XML
                        string sReportNumber = string.Empty;

                        if (xmlReader.ReadToFollowing("ReportNumber"))
                        {
                            sReportNumber = xmlReader.ReadString();
                            iNextReportNumber = int.Parse(sReportNumber);
                        }
                    }
                }

                return (iNextReportNumber += 1);

            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                if(xmlReader != null)
                    xmlReader.Close();
            }
            
        }


        public void DeleteOldestXmlFile()
        {

            try
            {

                DirectoryInfo dirInfo = new DirectoryInfo(Common.PATH_REPORTS);

                IComparer IDateComparer = new DateComparerAsc();

                FileInfo[] fInfo = dirInfo.GetFiles("*.xml");

                if (fInfo.Length > 0)
                {
                    Array.Sort(fInfo, IDateComparer);

                    fInfo[0].Delete();
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void DeleteXMLReport(string XMLFullName)
        {

            try
            {
                FileInfo fInfo = new FileInfo(XMLFullName);

                fInfo.Delete();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        
        }

        public void DeleteAllXMLReport()
        {

            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(Common.PATH_REPORTS);
                
                FileInfo[] fiXmlReports = dirInfo.GetFiles("*.xml");

                if(fiXmlReports.Length > 0)
                {
                    foreach (FileInfo fileInfo in fiXmlReports)
                    {
                        fileInfo.Delete();
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }



        private void CheckReportFolder()
        {

            DirectoryInfo dirInfo = new DirectoryInfo(Common.PATH_REPORTS);

            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }

        }

    }
}
